﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using System.IO;

public partial class teacher_updateTeacher : System.Web.UI.Page
{
    db_teacher teacher = new db_teacher();
    protected void Page_Load(object sender, EventArgs e)
    {
        Server.Execute("checkTeach.aspx");  // 此处用户权限验证

        
        //string teacherId = Request.QueryString["id"].ToString();
        string teacherId = Session["teacherID"].ToString();
        DataSet ds = teacher.selectTeacherONE(teacherId);
        if (!IsPostBack)
        {
            lb_Id.Text = ds.Tables[0].Rows[0]["teacherId"].ToString().Trim();
            tb_name.Text = ds.Tables[0].Rows[0]["teacherName"].ToString().Trim();
            tb_about.Text = ds.Tables[0].Rows[0]["about"].ToString().Trim();
            tb_picUrl.Text = ds.Tables[0].Rows[0]["picUrl"].ToString().Trim();
            img_pic.ImageUrl = tb_picUrl.Text;
        }
    }
    protected void btn_ok_Click(object sender, EventArgs e)
    {

        if (Session["teacherID"] == null)
        {
            Response.Write("<script language=javascript>alert('没有权限！');</script>");
            return;
        }

        string id = lb_Id.Text;
        string name = tb_name.Text;
        string about = tb_about.Text;
        string pic = tb_picUrl.Text;
        if (teacher.updateTeacher(id, name, about, pic, "0"))
        {
            Response.Write("<script language=javascript>alert('修改成功！');</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('失败！')</script>");
        }
    }
    protected void btn_upload_Click(object sender, EventArgs e)
    {
        try
        {
            if (fud_img.PostedFile.FileName == "")
            {
                Response.Write("<script language=javascript>alert('请选择上传文件！');</script>");
                return;
            }
            else if (lb_Id.Text == "")
            {
                Response.Write("<script language=javascript>alert('教师编号获取失败！');</script>");
                return;
            }
            else
            {
                string filepath = fud_img.PostedFile.FileName;  //得到的是文件的完整路径,包括文件名，如：C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg 
                if (Tools.IsAllowedExtension(fud_img))
                {
                    if (fud_img.FileContent.Length / 1024 < 1000)//检查图片大小
                    {
                        string fileExt = filepath.Substring(filepath.LastIndexOf("."));//.jpg 
                        String newFileName = lb_Id.Text + DateTime.Now.ToString("_MMdd_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
                        string serverpath = Server.MapPath("~/upload/teacherPic/");//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg 
                        if (!Directory.Exists(serverpath))
                        {
                            Directory.CreateDirectory(serverpath);  // 复制过来的代码改了这里，自动创建目录
                        }
                        fud_img.PostedFile.SaveAs(serverpath + newFileName);//将上传的文件另存为 
                        tb_picUrl.Text = "/upload/teacherPic/" + newFileName;
                        img_pic.ImageUrl = "~/upload/teacherPic/" + newFileName;
                        Response.Write("<script language=javascript>alert('上传成功！');</script>");
                    }
                    else
                    {
                        Response.Write("<script language=javascript>alert('上传图片文件过大，请上传小于1M的图片文件');</script>");
                    }
                }
                else
                {
                    Response.Write("<script language=javascript>alert('上传文件格式不正确！必须为：.jpg,.gif,bmp,png格式');</script>");
                }
            }
        }
        catch
        {
            Response.Write("<script language=javascript>alert('上传发生错误！');</script>");
        }
    }
}